package templates

import (
	"github.com/devops-smartbot/devops-smartbot/services/workflow-service/internal/models"
)

// GetBuiltinTemplates returns built-in workflow templates
func GetBuiltinTemplates() []models.WorkflowTemplate {
	return []models.WorkflowTemplate{
		{
			ID:          "alert-to-ticket",
			Name:        "Alert to Ticket",
			Description: "Automatically create a ticket when a critical alert is received",
			Category:    "alert-management",
			TriggerType: models.TriggerTypeAlert,
			TriggerConfig: models.TriggerConfig{
				AlertSeverity: "critical",
			},
			Steps: []models.WorkflowStep{
				{
					ID:         "step-1",
					Name:       "Create Ticket",
					ActionType: models.ActionTypeCreateTicket,
					Parameters: map[string]string{
						"title":       "{{alert.name}}",
						"description": "{{alert.description}}",
						"priority":    "high",
						"type":        "incident",
					},
				},
				{
					ID:         "step-2",
					Name:       "Send Notification",
					ActionType: models.ActionTypeSendNotification,
					Parameters: map[string]string{
						"channel": "slack",
						"message": "New ticket created: {{ticket.id}}",
					},
					DependsOn: []string{"step-1"},
				},
			},
		},
		{
			ID:          "ticket-notification",
			Name:        "Ticket Status Notification",
			Description: "Send notification when ticket status changes",
			Category:    "ticket-management",
			TriggerType: models.TriggerTypeTicket,
			Steps: []models.WorkflowStep{
				{
					ID:         "step-1",
					Name:       "Send Email",
					ActionType: models.ActionTypeSendNotification,
					Parameters: map[string]string{
						"channel": "email",
						"subject": "Ticket {{ticket.id}} status changed",
						"body":    "Status: {{ticket.status}}",
					},
				},
			},
		},
		{
			ID:          "scheduled-health-check",
			Name:        "Scheduled Health Check",
			Description: "Perform health check on schedule",
			Category:    "monitoring",
			TriggerType: models.TriggerTypeSchedule,
			TriggerConfig: models.TriggerConfig{
				CronExpression: "0 */5 * * * *", // Every 5 minutes
			},
			Steps: []models.WorkflowStep{
				{
					ID:         "step-1",
					Name:       "HTTP Health Check",
					ActionType: models.ActionTypeHTTPRequest,
					Parameters: map[string]string{
						"method": "GET",
						"url":    "{{health_check_url}}",
					},
				},
				{
					ID:         "step-2",
					Name:       "Alert on Failure",
					ActionType: models.ActionTypeCondition,
					Parameters: map[string]string{
						"condition": "{{step-1.status_code}} != 200",
						"action":    "send_notification",
					},
					DependsOn: []string{"step-1"},
				},
			},
		},
		{
			ID:          "auto-remediation",
			Name:        "Auto Remediation",
			Description: "Automatically restart service on alert",
			Category:    "remediation",
			TriggerType: models.TriggerTypeAlert,
			TriggerConfig: models.TriggerConfig{
				AlertName: "ServiceDown",
			},
			Steps: []models.WorkflowStep{
				{
					ID:         "step-1",
					Name:       "Run Restart Script",
					ActionType: models.ActionTypeRunScript,
					Parameters: map[string]string{
						"script": "restart-service.sh",
						"args":   "{{alert.instance}}",
					},
				},
				{
					ID:         "step-2",
					Name:       "Wait",
					ActionType: models.ActionTypeWait,
					Parameters: map[string]string{
						"duration": "30s",
					},
					DependsOn: []string{"step-1"},
				},
				{
					ID:         "step-3",
					Name:       "Verify Service",
					ActionType: models.ActionTypeHTTPRequest,
					Parameters: map[string]string{
						"method": "GET",
						"url":    "http://{{alert.instance}}/health",
					},
					DependsOn: []string{"step-2"},
				},
				{
					ID:         "step-4",
					Name:       "Notify Result",
					ActionType: models.ActionTypeSendNotification,
					Parameters: map[string]string{
						"channel": "slack",
						"message": "Remediation {{#if step-3.success}}succeeded{{else}}failed{{/if}}",
					},
					DependsOn:       []string{"step-3"},
					ContinueOnError: true,
				},
			},
		},
	}
}

// GetTemplateByID returns a template by ID
func GetTemplateByID(id string) *models.WorkflowTemplate {
	templates := GetBuiltinTemplates()
	for _, t := range templates {
		if t.ID == id {
			return &t
		}
	}
	return nil
}

// GetTemplatesByCategory returns templates by category
func GetTemplatesByCategory(category string) []models.WorkflowTemplate {
	if category == "" {
		return GetBuiltinTemplates()
	}

	templates := GetBuiltinTemplates()
	result := []models.WorkflowTemplate{}
	for _, t := range templates {
		if t.Category == category {
			result = append(result, t)
		}
	}
	return result
}
